package jh.com.jhappbf.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.android.volley.Request.Priority;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import androidx.fragment.app.Fragment;
import jh.com.jhappbf.R;
import jh.com.jhappbf.httputils.VolleyCallWebService;
import jh.com.jhappbf.httputils.VolleySingleton;
import jh.com.jhappbf.utils.JsonHelper;

/**
 * @author Administrator
 */
public class AssayDetail extends Fragment {
    private TextView mTxt;
    private TextView mTitle;
    private TextView mTxtSetting1;
    private TextView mTxtSetting2;
    private ImageView mSplitView;
    private ProgressBar mProgressBar;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.assay_detail_view, null);
        mTxt = v.findViewById(R.id.assay_detail_content);
        mProgressBar = v.findViewById(R.id.assay_detail_bar);
        mTitle = getActivity().findViewById(R.id.ui_singleActivity_title);
        mTitle.setText("化验详细信息");
        mTxtSetting1 = getActivity().findViewById(R.id.ui_singleActivity_txt1);
        mTxtSetting1.setVisibility(View.GONE);
        mTxtSetting2 = getActivity().findViewById(R.id.ui_singleActivity_txt2);
        mTxtSetting2.setVisibility(View.GONE);
        mSplitView = getActivity().findViewById(R.id.ui_singleActivity_line);
        mSplitView.setVisibility(View.GONE);
        getData();
        return v;
    }

    /**
     * 获取数据
     */
    private void getData() {
        mProgressBar.setVisibility(View.VISIBLE);
        mTxt.setVisibility(View.GONE);
        Intent i = getActivity().getIntent();
        if (i == null) {
            return;
        }
        String saverId = i.getStringExtra("saverId");
        String cargoName = i.getStringExtra("cargoname");
        if (saverId == null || cargoName == null ||
                saverId.length() == 0 || cargoName.length() == 0) {
            mTxt.setText("参数格式不正确，未能查询到数据!");
            return;
        }

        String methodName = "getAssayDetail";
        ArrayList<String> param = new ArrayList<>();
        ArrayList<String> value = new ArrayList<>();
        param.add("saverId");
        param.add("cargoname");
        value.add(saverId);
        value.add(cargoName);

        Listener<String> listener = new Listener<String>() {

            @Override
            public void onResponse(String response) {
                mProgressBar.setVisibility(View.GONE);
                mTxt.setVisibility(View.VISIBLE);
                JSONObject obj = JsonHelper.getJsonObject(response);
                if (obj != null) {
                    String result = "";
                    try {
                        result = obj.getString("result");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    mTxt.setText(result);
                }
            }
        };

        ErrorListener errorListener = new ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                mProgressBar.setVisibility(View.GONE);
                mTxt.setVisibility(View.VISIBLE);
                mTxt.setText("获取数据异常:" + error);
            }
        };

        VolleyCallWebService httpService = new VolleyCallWebService(methodName, param, value, listener, errorListener, Priority.NORMAL);
        VolleySingleton volley = VolleySingleton.getVolleyInstance(getActivity().getApplicationContext());
        volley.addRequest(httpService);

    }
}
